<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Mysql," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="MySQL的基本语法left JOIN  左表匹配右表
有没有内容全部匹配  

12345SELECT Persons.LastName, Orders.OrderNoFROM PersonsINNER JOIN OrdersON Persons.Id_P=Orders.Id_PORDER BY Persons.LastName

只输出右边的表有内容的">
<meta property="og:type" content="article">
<meta property="og:title" content="Mysql最常用的语法">
<meta property="og:url" content="http://yoursite.com/2017/05/02/Mysql最常用的语法/index.html">
<meta property="og:site_name" content="起今知行 | 思考者">
<meta property="og:description" content="MySQL的基本语法left JOIN  左表匹配右表
有没有内容全部匹配  

12345SELECT Persons.LastName, Orders.OrderNoFROM PersonsINNER JOIN OrdersON Persons.Id_P=Orders.Id_PORDER BY Persons.LastName

只输出右边的表有内容的">
<meta property="og:image" content="http://ois8kgfyr.bkt.clouddn.com/image/blog/Snip20170105_1.png">
<meta property="og:image" content="http://ois8kgfyr.bkt.clouddn.com/image/blog/Snip20170105_2.png">
<meta property="og:image" content="http://ois8kgfyr.bkt.clouddn.com/image/blog/Snip20170105_3.png">
<meta property="og:updated_time" content="2017-05-02T04:41:32.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Mysql最常用的语法">
<meta name="twitter:description" content="MySQL的基本语法left JOIN  左表匹配右表
有没有内容全部匹配  

12345SELECT Persons.LastName, Orders.OrderNoFROM PersonsINNER JOIN OrdersON Persons.Id_P=Orders.Id_PORDER BY Persons.LastName

只输出右边的表有内容的">
<meta name="twitter:image" content="http://ois8kgfyr.bkt.clouddn.com/image/blog/Snip20170105_1.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/05/02/Mysql最常用的语法/"/>





  <title> Mysql最常用的语法 | 起今知行 | 思考者 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">起今知行 | 思考者</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">自我管理，知识管理，时间管理，阅读</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocapitalize="off" autocomplete="off" autocorrect="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/05/02/Mysql最常用的语法/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Winsion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="起今知行 | 思考者">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Mysql最常用的语法
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-02T11:50:52+08:00">
                2017-05-02
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/教程/" itemprop="url" rel="index">
                    <span itemprop="name">教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2017/05/02/Mysql最常用的语法/" class="leancloud_visitors" data-flag-title="Mysql最常用的语法">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数 </span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计</span>
                
                <span title="字数统计">
                  982字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长</span>
                
                <span title="阅读时长">
                  4分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="MySQL的基本语法"><a href="#MySQL的基本语法" class="headerlink" title="MySQL的基本语法"></a>MySQL的基本语法</h1><h2 id="left-JOIN-左表匹配右表"><a href="#left-JOIN-左表匹配右表" class="headerlink" title="left JOIN  左表匹配右表"></a>left JOIN  左表匹配右表</h2><ul>
<li>有没有内容全部匹配  </li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">SELECT Persons.LastName, Orders.OrderNo</div><div class="line">FROM Persons</div><div class="line">INNER JOIN Orders</div><div class="line">ON Persons.Id_P=Orders.Id_P</div><div class="line">ORDER BY Persons.LastName</div></pre></td></tr></table></figure>
<ul>
<li>只输出右边的表有内容的<a id="more"></a>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo</div><div class="line">FROM Persons</div><div class="line">INNER JOIN Orders</div><div class="line">ON Persons.Id_P=Orders.Id_P</div><div class="line">ORDER BY Persons.LastName</div></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="RIGHT-JOIN-关键字会右表-table-name2-那里返回所有的行，即使在左表-table-name1-中没有匹配的行。"><a href="#RIGHT-JOIN-关键字会右表-table-name2-那里返回所有的行，即使在左表-table-name1-中没有匹配的行。" class="headerlink" title="RIGHT JOIN 关键字会右表 (table_name2) 那里返回所有的行，即使在左表 (table_name1) 中没有匹配的行。"></a>RIGHT JOIN 关键字会右表 (table_name2) 那里返回所有的行，即使在左表 (table_name1) 中没有匹配的行。</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">SELECT column_name(s)</div><div class="line">FROM table_name1</div><div class="line">RIGHT JOIN table_name2 </div><div class="line">ON table_name1.column_name=table_name2.column_name</div></pre></td></tr></table></figure>
<h2 id="SQL-FULL-JOIN-关键字只要其中某个表存在匹配，FULL-JOIN-关键字就会返回行。"><a href="#SQL-FULL-JOIN-关键字只要其中某个表存在匹配，FULL-JOIN-关键字就会返回行。" class="headerlink" title="SQL FULL JOIN 关键字只要其中某个表存在匹配，FULL JOIN 关键字就会返回行。"></a>SQL FULL JOIN 关键字只要其中某个表存在匹配，FULL JOIN 关键字就会返回行。</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">SELECT column_name(s)</div><div class="line">FROM table_name1</div><div class="line">FULL JOIN table_name2 </div><div class="line">ON table_name1.column_name=table_name2.column_name</div></pre></td></tr></table></figure>
<h2 id="UNION-操作符用于合并两个或多个-SELECT-语句的结果集。"><a href="#UNION-操作符用于合并两个或多个-SELECT-语句的结果集。" class="headerlink" title="UNION 操作符用于合并两个或多个 SELECT 语句的结果集。"></a>UNION 操作符用于合并两个或多个 SELECT 语句的结果集。</h2><ul>
<li>请注意，UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时，每条 SELECT 语句中的列的顺序必须相同。</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">SELECT column_name(s) FROM table_name1</div><div class="line">UNION</div><div class="line">SELECT column_name(s) FROM table_name2</div></pre></td></tr></table></figure>
<h2 id="删除表"><a href="#删除表" class="headerlink" title="删除表"></a>删除表</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">drop table `MYFirstTable`</div></pre></td></tr></table></figure>
<h2 id="删除数据库"><a href="#删除数据库" class="headerlink" title="删除数据库"></a>删除数据库</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">drop database MY_db</div></pre></td></tr></table></figure>
<h2 id="复制表"><a href="#复制表" class="headerlink" title="复制表"></a>复制表</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">creat table `NewMYFirstTable` select *from `OldMYFirstTable`</div></pre></td></tr></table></figure>
<h2 id="修改数据库的编码规则"><a href="#修改数据库的编码规则" class="headerlink" title="修改数据库的编码规则"></a>修改数据库的编码规则</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">alter database mydb character set utf8;</div></pre></td></tr></table></figure>
<h2 id="创建数据库的时候设置编码规则"><a href="#创建数据库的时候设置编码规则" class="headerlink" title="创建数据库的时候设置编码规则"></a>创建数据库的时候设置编码规则</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">CREATE DATABASE db_name DEFAULT CHARACTER SET utf8</div></pre></td></tr></table></figure>
<h2 id="创建表-lt-加约束"><a href="#创建表-lt-加约束" class="headerlink" title="创建表&lt;加约束)"></a>创建表&lt;加约束)</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">CREATE TABLE Persons</div><div class="line">(</div><div class="line">Id_P int NOT NULL,(不接受空值)</div><div class="line">LastName varchar(255) NOT NULL,</div><div class="line">FirstName varchar(255),</div><div class="line">Address varchar(255),</div><div class="line">City varchar(255)</div><div class="line">UNIQUE (Id_P) **约束**</div><div class="line"></div><div class="line">PRIMARY KEY (Id_P) **设置主键**</div><div class="line">)</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">SQL UNIQUE 约束</div><div class="line">UNIQUE 约束唯一标识数据库表中的每条记录。</div><div class="line">UNIQUE 和 PRIMARY KEY 约束均为列或列集合提供了唯一性的保证。</div></pre></td></tr></table></figure>
<ul>
<li><p>PRIMARY KEY 约束唯一标识数据库表中的每条记录。主键必须包含唯一的值。主键列不能包含 NULL 值。每个表都应该有一个主键，并且每个表只能有一个主键。</p>
</li>
<li><p>如果表已经存在,择设置主键</p>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">ALTER TABLE Persons</div><div class="line">ADD PRIMARY KEY (Id_P)</div></pre></td></tr></table></figure>
<ul>
<li>撤销主键约束</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">ALTER TABLE Persons</div><div class="line">DROP PRIMARY KEY</div></pre></td></tr></table></figure>
<ul>
<li>CHECK 约束规定 “Id_P” 列必须只包含大于 0 的整数</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">CHECK (Id_P&gt;0)</div></pre></td></tr></table></figure>
<ul>
<li>更新一个数值</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">update setUpdefault set neme = &apos;hahhahfrewgerwg&apos; WHERE neme = &apos;hahhah&apos;</div></pre></td></tr></table></figure>
<h2 id="创建索引"><a href="#创建索引" class="headerlink" title="创建索引"></a>创建索引</h2><ul>
<li>CREATE INDEX 语句用于在表中创建索引。在不读取整个表的情况下，索引使数据库应用程序可以更快地查找数据。<br> 注释：更新一个包含索引的表需要比更新一个没有索引的表更多的时间，这是由于索引本身也需要更新。因此，理想的做法是仅仅在常常被搜索的列（以及表）上面创建索引。</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">CREATE INDEX index_name</div><div class="line">ON table_name (column_name)</div></pre></td></tr></table></figure>
<ul>
<li>删除索引</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">DROP INDEX index_name ON table_name</div></pre></td></tr></table></figure>
<h2 id="删除表的数据"><a href="#删除表的数据" class="headerlink" title="删除表的数据"></a>删除表的数据</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">TRUNCATE TABLE 表名称</div><div class="line"></div><div class="line">delete from tableName</div><div class="line">//删除部分数据</div><div class="line">DELETE FROM table1 WHERE ...;</div></pre></td></tr></table></figure>
<h2 id="添加列-删除列-改变列的数据类型"><a href="#添加列-删除列-改变列的数据类型" class="headerlink" title="添加列,删除列,改变列的数据类型"></a>添加列,删除列,改变列的数据类型</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">如需在表中添加列，请使用下列语法:</div><div class="line">ALTER TABLE table_name</div><div class="line">ADD column_name datatype</div><div class="line"></div><div class="line">要删除表中的列，请使用下列语法：</div><div class="line">ALTER TABLE table_name </div><div class="line">DROP COLUMN column_name</div><div class="line"></div><div class="line"></div><div class="line">要改变表中列的数据类型，请使用下列语法：</div><div class="line">ALTER TABLE table_name</div><div class="line">ALTER COLUMN column_name datatype</div></pre></td></tr></table></figure>
<p>//设置让主键自增<br>我们通常希望在每次插入新记录时，自动地创建主键字段的值。<br>我们可以在表中创建一个 auto-increment 字段。<br>下列 SQL 语句把 “Persons” 表中的 “P_Id” 列定义为 auto-increment 主键：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">alter table S_Province modify `ProvinceID` integer auto_increment</div></pre></td></tr></table></figure>
<h2 id="添加视图"><a href="#添加视图" class="headerlink" title="添加视图"></a>添加视图</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">CREATE VIEW [Current Product List] AS</div><div class="line">SELECT ProductID,ProductName</div><div class="line">FROM Products</div><div class="line">WHERE Discontinued=No</div><div class="line">我们可以查询上面这个视图：</div><div class="line">SELECT * FROM [Current Product List]</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"></div></pre></td></tr></table></figure>
<h2 id="时间的格式"><a href="#时间的格式" class="headerlink" title="时间的格式"></a>时间的格式</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">MySQL 使用下列数据类型在数据库中存储日期或日期/时间值：</div><div class="line">DATE - 格式 YYYY-MM-DD</div><div class="line">DATETIME - 格式: YYYY-MM-DD HH:MM:SS</div><div class="line">TIMESTAMP - 格式: YYYY-MM-DD HH:MM:SS</div><div class="line">YEAR - 格式 YYYY 或 YY</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">SELECT * FROM Orders WHERE OrderDate=&apos;2008-12-26&apos;</div></pre></td></tr></table></figure>
<h2 id="NULL值"><a href="#NULL值" class="headerlink" title="NULL值"></a>NULL值</h2><p>我们如何仅仅选取在 “Address” 列中带有 NULL 值的记录呢？<br>我们必须使用 IS NULL 操作符：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">SELECT LastName,FirstName,Address FROM Persons</div><div class="line">WHERE Address IS NULL</div></pre></td></tr></table></figure>
<h2 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h2><p><img src="http://ois8kgfyr.bkt.clouddn.com/image/blog/Snip20170105_1.png" alt=""><br><img src="http://ois8kgfyr.bkt.clouddn.com/image/blog/Snip20170105_2.png" alt=""><br><img src="http://ois8kgfyr.bkt.clouddn.com/image/blog/Snip20170105_3.png" alt=""></p>
<h2 id="从第几行哦第几行"><a href="#从第几行哦第几行" class="headerlink" title="从第几行哦第几行"></a>从第几行哦第几行</h2><p>limit 2， 4</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Mysql/" rel="tag"># Mysql</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/04/28/程序员最正确的定义/" rel="next" title="程序员最正确的定义">
                <i class="fa fa-chevron-left"></i> 程序员最正确的定义
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/05/02/java注意事项/" rel="prev" title="java注意事项">
                java注意事项 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.gif"
               alt="Winsion" />
          <p class="site-author-name" itemprop="name">Winsion</p>
           
              <p class="site-description motion-element" itemprop="description">知识管理，时间管理，自我管理，阅读，语音写作，思维导图，Wiz，TimeMeter</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">35</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">11</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">31</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/winsions" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://coding.net/u/winsions" target="_blank" title="Coding">
                  
                    <i class="fa fa-fw fa-coding"></i>
                  
                  Coding
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/winsions" target="_blank" title="微博">
                  
                    <i class="fa fa-fw fa-weibo"></i>
                  
                  微博
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.douban.com/people/winsions" target="_blank" title="豆瓣">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  豆瓣
                </a>
              </span>
            
          
        </div>

        
        

        
        

        
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=436514312&auto=1&height=66"></iframe>


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#MySQL的基本语法"><span class="nav-number">1.</span> <span class="nav-text">MySQL的基本语法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#left-JOIN-左表匹配右表"><span class="nav-number">1.1.</span> <span class="nav-text">left JOIN  左表匹配右表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#RIGHT-JOIN-关键字会右表-table-name2-那里返回所有的行，即使在左表-table-name1-中没有匹配的行。"><span class="nav-number">1.2.</span> <span class="nav-text">RIGHT JOIN 关键字会右表 (table_name2) 那里返回所有的行，即使在左表 (table_name1) 中没有匹配的行。</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SQL-FULL-JOIN-关键字只要其中某个表存在匹配，FULL-JOIN-关键字就会返回行。"><span class="nav-number">1.3.</span> <span class="nav-text">SQL FULL JOIN 关键字只要其中某个表存在匹配，FULL JOIN 关键字就会返回行。</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#UNION-操作符用于合并两个或多个-SELECT-语句的结果集。"><span class="nav-number">1.4.</span> <span class="nav-text">UNION 操作符用于合并两个或多个 SELECT 语句的结果集。</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#删除表"><span class="nav-number">1.5.</span> <span class="nav-text">删除表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#删除数据库"><span class="nav-number">1.6.</span> <span class="nav-text">删除数据库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#复制表"><span class="nav-number">1.7.</span> <span class="nav-text">复制表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#修改数据库的编码规则"><span class="nav-number">1.8.</span> <span class="nav-text">修改数据库的编码规则</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建数据库的时候设置编码规则"><span class="nav-number">1.9.</span> <span class="nav-text">创建数据库的时候设置编码规则</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建表-lt-加约束"><span class="nav-number">1.10.</span> <span class="nav-text">创建表<加约束)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建索引"><span class="nav-number">1.11.</span> <span class="nav-text">创建索引</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#删除表的数据"><span class="nav-number">1.12.</span> <span class="nav-text">删除表的数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#添加列-删除列-改变列的数据类型"><span class="nav-number">1.13.</span> <span class="nav-text">添加列,删除列,改变列的数据类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#添加视图"><span class="nav-number">1.14.</span> <span class="nav-text">添加视图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#时间的格式"><span class="nav-number">1.15.</span> <span class="nav-text">时间的格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#NULL值"><span class="nav-number">1.16.</span> <span class="nav-text">NULL值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据类型"><span class="nav-number">1.17.</span> <span class="nav-text">数据类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#从第几行哦第几行"><span class="nav-number">1.18.</span> <span class="nav-text">从第几行哦第几行</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Winsion</span>
</div>


<div >
 <br/>
 <br/>
<script type="text/javascript" src="http://tajs.qq.com/stats?sId=61514183" charset="UTF-8"></script>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  






  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  


  




	





  





  





  






  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
      search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.popup').toggle();
    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';
      $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = $( "entry", xmlResponse ).map(function() {
            return {
              title: $( "title", this ).text(),
              content: $("content",this).text(),
              url: $( "url" , this).text()
            };
          }).get();
          var $input = document.getElementById(search_id);
          var $resultContent = document.getElementById(content_id);
          $input.addEventListener('input', function(){
            var matchcounts = 0;
            var str='<ul class=\"search-result-list\">';
            var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
            $resultContent.innerHTML = "";
            if (this.value.trim().length > 1) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var content_index = [];
                var data_title = data.title.trim().toLowerCase();
                var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                var data_url = decodeURIComponent(data.url);
                var index_title = -1;
                var index_content = -1;
                var first_occur = -1;
                // only match artiles with not empty titles and contents
                if(data_title != '') {
                  keywords.forEach(function(keyword, i) {
                    index_title = data_title.indexOf(keyword);
                    index_content = data_content.indexOf(keyword);
                    if( index_title >= 0 || index_content >= 0 ){
                      isMatch = true;
                      if (i == 0) {
                        first_occur = index_content;
                      }
                    }

                  });
                }
                // show search results
                if (isMatch) {
                  matchcounts += 1;
                  str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                  var content = data.content.trim().replace(/<[^>]+>/g,"");
                  if (first_occur >= 0) {
                    // cut out 100 characters
                    var start = first_occur - 20;
                    var end = first_occur + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if(start == 0){
                      end = 50;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    var match_content = content.substring(start, end);
                    // highlight all keywords
                    keywords.forEach(function(keyword){
                      var regS = new RegExp(keyword, "gi");
                      match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                    });

                    str += "<p class=\"search-result\">" + match_content +"...</p>"
                  }
                  str += "</li>";
                }
              })};
            str += "</ul>";
            if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
            if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
            $resultContent.innerHTML = str;
          });
          proceedsearch();
        }
      });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("aR58NiUI6UxE1EeSHEMgVuI8-gzGzoHsz", "Tu5BQCGEuL0X0IchB5aYRdxk");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  


<a href="https://github.com/winsions"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/e7bbb0521b397edbd5fe43e7f760759336b5e05f/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677265656e5f3030373230302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_green_007200.png"></a></html>
